package com.lhq.tag_tales.common;

import lombok.Data;

import java.io.Serializable;

/**
 * 基础响应类，用于封装API的响应结果
 * @param <T> 响应数据的类型
 */
@Data
public class  BaseResponse<T> implements Serializable {
    /**
     * 状态码
     */
    private int code;
    /**
     * 提示信息
     */
    private String message;
    /**
     * 数据
     */
    private T data;
    /**
     * 描述
     */
    private String description;

    /**
     * 创建一个成功的响应
     * @param <T> 响应数据的类型
     * @return 成功的响应对象
     */
    public static<T> BaseResponse<T> success(){
        BaseResponse<T> response = new BaseResponse<>();
        response.setCode(ErrorCodeEnum.SUCCESS.getCode());
        response.setMessage(ErrorCodeEnum.SUCCESS.getMessage());
        response.setDescription(ErrorCodeEnum.SUCCESS.getDescription());
        return response;
    }

    public static<T> BaseResponse<T> success(String description){
        BaseResponse<T> response = new BaseResponse<>();
        response.setCode(ErrorCodeEnum.SUCCESS.getCode());
        response.setMessage(ErrorCodeEnum.SUCCESS.getMessage());
        response.setDescription(description);
        return response;
    }


    /**
     * 创建一个携带数据的成功的响应
     * @param data 响应的数据
     * @param <T> 响应数据的类型
     * @return 成功的响应对象
     */
    public static<T> BaseResponse<T> success(T data){
        BaseResponse<T> response = new BaseResponse<>();
        response.setCode(ErrorCodeEnum.SUCCESS.getCode());
        response.setData(data);
        response.setMessage("ok");
        response.setDescription("成功");
        return response;
    }


    public static<T> BaseResponse<T> error(ErrorCodeEnum code,String description){
        BaseResponse<T> response = new BaseResponse<>();
        response.setMessage(code.getMessage());
        response.setCode(code.getCode());
        response.setDescription(description);
        return response;
    }

    public static<T> BaseResponse<T> error(int code,String message,String description){
        BaseResponse<T> response = new BaseResponse<>();
        response.setMessage(message);
        response.setCode(code);
        response.setDescription(description);
        return response;
    }
}
